﻿using System.Net.Http.Headers;
using System.Text;
using iTeamyou.LTS.Core.Models;
using iTeamyou.LTS.Core.Services;
using iTeamyou.LTS.DataJob;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using NLog;
using NLog.Extensions.Logging;
using Renci.SshNet;
using static System.Net.WebRequestMethods;

var sharedService = new SharedService();
var services = new ServiceCollection();
services.AddScoped<DbContext, MainDBContext>()
    .AddScoped<ProductService>()
    .AddScoped<SupplierService>()
    .AddScoped<BatchService>()
    .AddScoped<SyncService>();

services.AddDbContext<MainDBContext>(options => options.UseMySql(sharedService.GetConnectionString("MainDB"), new MySqlServerVersion(new Version())));
services.AddLogging(builder =>
{
    builder.SetMinimumLevel(Microsoft.Extensions.Logging.LogLevel.Information);
    builder.AddNLog("nlog.config");
});

var serviceProvider = services.BuildServiceProvider();
var productService = serviceProvider.GetService<ProductService>();
var supplierService = serviceProvider.GetService<SupplierService>();
var batchService = serviceProvider.GetService<BatchService>();
var syncService = serviceProvider.GetService<SyncService>();
var logService = LogManager.GetCurrentClassLogger();

Console.WriteLine("Start");

var path = sharedService.GetConfig("FilePath");
var bakPath = sharedService.GetConfig("BakPath");
var mode = sharedService.GetConfig("SyncMode");
var host = sharedService.GetConfig("Host");
var username = sharedService.GetConfig("Username");
var password = sharedService.GetConfig("Password");
var remoteFilePath = sharedService.GetConfig("RemoteFilePath");
var job = new FileSync(productService, supplierService, batchService, logService, syncService);

//路径转换
//job.Sync(path.Replace("/", "\\"), mode, "supplierUpdate");

//job.Sync(path, mode, "supplierUpdate");
//if (args.Contains("--supplierUpdate"))
//    job.Sync(path, mode, "supplierUpdate");
//else if (args.Contains("--supplier"))
//    job.Sync(path, mode, "supplier");
//else if (args.Contains("--product"))
//    job.Sync(path, mode, "product");
//else if (args.Contains("--batch"))
//    job.Sync(path, mode, "batch");
//else
//    job.Sync(path, mode, "db");

//job.Sync(path, bakPath);

//远程文件拉取到本地
if (args[0] == "file")
{
    job.DownloadFile(host, username, password, remoteFilePath, path, args[1]);
}
//主程序
job.Sync(args[0], args[1], args[2], args[3]);

//Console.ReadLine();
//job.Sync(path, mode, "supplierUpdate");
//job.Sync(path, mode, "supplier");
//job.Sync("D:\\LTS\\ftp\\amasterandfsinfo", mode, "product");
//job.Sync("D:\\LTS\\ftp\\wms", mode, "batch");